﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace YJ.Data.MySql
{
    public class AppLibraryButtons1 : YJ.Data.Interface.IAppLibraryButtons1
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public AppLibraryButtons1()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.AppLibraryButtons1实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.AppLibraryButtons1 model)
        {
            string sql = @"INSERT INTO applibrarybuttons1
				(ID,AppLibraryID,ButtonID,Name,Events,Ico,Sort,Type,ShowType,IsValidateShow) 
				VALUES(@ID,@AppLibraryID,@ButtonID,@Name,@Events,@Ico,@Sort,@Type,@ShowType,@IsValidateShow)";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID },
				new MySqlParameter("@AppLibraryID", MySqlDbType.VarChar, 36){ Value = model.AppLibraryID },
				model.ButtonID == null ? new MySqlParameter("@ButtonID", MySqlDbType.VarChar, 36) { Value = DBNull.Value } : new MySqlParameter("@ButtonID", MySqlDbType.VarChar, 36) { Value = model.ButtonID },
				new MySqlParameter("@Name", MySqlDbType.Text, -1){ Value = model.Name },
				new MySqlParameter("@Events", MySqlDbType.Text, -1){ Value = model.Events },
				model.Ico == null ? new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = model.Ico },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort },
				new MySqlParameter("@Type", MySqlDbType.Int32, 11){ Value = model.Type },
				new MySqlParameter("@ShowType", MySqlDbType.Int32, 11){ Value = model.ShowType },
				new MySqlParameter("@IsValidateShow", MySqlDbType.Int32, 11){ Value = model.IsValidateShow }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.AppLibraryButtons1实体类</param>
        public int Update(YJ.Data.Model.AppLibraryButtons1 model)
        {
            string sql = @"UPDATE applibrarybuttons1 SET 
				AppLibraryID=@AppLibraryID,ButtonID=@ButtonID,Name=@Name,Events=@Events,Ico=@Ico,Sort=@Sort,Type=@Type,ShowType=@ShowType,IsValidateShow=@IsValidateShow
				WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@AppLibraryID", MySqlDbType.VarChar, 36){ Value = model.AppLibraryID },
				model.ButtonID == null ? new MySqlParameter("@ButtonID", MySqlDbType.VarChar, 36) { Value = DBNull.Value } : new MySqlParameter("@ButtonID", MySqlDbType.VarChar, 36) { Value = model.ButtonID },
				new MySqlParameter("@Name", MySqlDbType.Text, -1){ Value = model.Name },
				new MySqlParameter("@Events", MySqlDbType.Text, -1){ Value = model.Events },
				model.Ico == null ? new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = model.Ico },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort },
				new MySqlParameter("@Type", MySqlDbType.Int32, 11){ Value = model.Type },
				new MySqlParameter("@ShowType", MySqlDbType.Int32, 11){ Value = model.ShowType },
				new MySqlParameter("@IsValidateShow", MySqlDbType.Int32, 11){ Value = model.IsValidateShow },
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM applibrarybuttons1 WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.AppLibraryButtons1> DataReaderToList(MySqlDataReader dataReader)
        {
            List<YJ.Data.Model.AppLibraryButtons1> List = new List<YJ.Data.Model.AppLibraryButtons1>();
            YJ.Data.Model.AppLibraryButtons1 model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.AppLibraryButtons1();
                model.ID = dataReader.GetString(0).ToGuid();
                model.AppLibraryID = dataReader.GetString(1).ToGuid();
                if (!dataReader.IsDBNull(2))
                    model.ButtonID = dataReader.GetString(2).ToGuid();
                model.Name = dataReader.GetString(3);
                model.Events = dataReader.GetString(4);
                if (!dataReader.IsDBNull(5))
                    model.Ico = dataReader.GetString(5);
                model.Sort = dataReader.GetInt32(6);
                model.Type = dataReader.GetInt32(7);
                model.ShowType = dataReader.GetInt32(8);
                model.IsValidateShow = dataReader.GetInt32(9);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.AppLibraryButtons1> GetAll()
        {
            string sql = "SELECT * FROM applibrarybuttons1";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.AppLibraryButtons1> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM applibrarybuttons1";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.AppLibraryButtons1 Get(Guid id)
        {
            string sql = "SELECT * FROM applibrarybuttons1 WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.AppLibraryButtons1> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }


        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByAppID(Guid id)
        {
            string sql = "DELETE FROM AppLibraryButtons1 WHERE AppLibraryID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }


        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.AppLibraryButtons1> GetAllByAppID(Guid id)
        {
            string sql = "SELECT * FROM AppLibraryButtons1 WHERE AppLibraryID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar){ Value = id }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.AppLibraryButtons1> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}